<?php 
/**
 * 自定义函数库
 */

/**
 * 查询所有数据
 * 
 */	
function getList($table, $where=array(),$orderby=null){
	$result = M($table)->where($where)->order($orderby)->select();
	return $result;
}

/**
 * 获得当前的域名
 *
 * @return  string
 */
function get_domain()
{
    /* 协议 */
    $protocol = (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) ? 'https://' : 'http://';

    /* 域名或IP地址 */
    if (isset($_SERVER['HTTP_X_FORWARDED_HOST']))
    {
        $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
    }
    elseif (isset($_SERVER['HTTP_HOST']))
    {
        $host = $_SERVER['HTTP_HOST'];
    }
    else
    {
        /* 端口 */
        if (isset($_SERVER['SERVER_PORT']))
        {
            $port = ':' . $_SERVER['SERVER_PORT'];

            if ((':80' == $port && 'http://' == $protocol) || (':443' == $port && 'https://' == $protocol))
            {
                $port = '';
            }
        }
        else
        {
            $port = '';
        }

        if (isset($_SERVER['SERVER_NAME']))
        {
            $host = $_SERVER['SERVER_NAME'] . $port;
        }
        elseif (isset($_SERVER['SERVER_ADDR']))
        {
            $host = $_SERVER['SERVER_ADDR'] . $port;
        }
    }

    return $protocol . $host;
}

/**
 * 查询一条数据
 * 
 */	
function getRow($table, $where=array()){
	$result = M($table)->where($where)->find();
	return $result;
}

/**
 * 获取字段
 * 
 */	
function getField($table, $field = null, $where=array()){
	$result = M($table)->field($field)->where($where)->select();
	return $result;
}

/**
 * 删除数据
 * 
 */	
function delete($table, $where=array()){
	$result = M($table)->where($where)->delete();
	return $result;
}

/**
 * 更新数据
 * 
 */	
function update($table, $save, $where=array()){
	$result = M($table)->where($where)->save($save);
	return $result;
}

/**
 * 添加数据
 * @return id
 */	
function add($table, $add){
	$result = M($table)->add($add);
	return $result;
}

/**
 * 统计数据
 * 
 */	
function getCount($table, $where){
	$result = M($table)->where($where)->count();
	return $result;
}

/**
 * 获取当前页面完整URL地址
 */
function getUrl() {
    $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
    $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
    $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
    $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
    return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
}

/**
 * 分页
 * count 总共条数
 * number  一页条数
 */
function page($count,$num=20){
	$Page       = new \Think\Page($count,$num);
    $Page -> setConfig('header','共%TOTAL_ROW%条');
    $Page -> setConfig('first','首页');
    $Page -> setConfig('last','共%TOTAL_PAGE%页');
    $Page -> setConfig('prev','上一页');
    $Page -> setConfig('next','下一页');
    $Page -> setConfig('link','indexpagenumb');
    $Page -> setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
    $show       = $Page->show();
    $info['page'] = $Page;
    $info['show'] = $show;

    return $info;
}

/**
 * 获取小区名
 * 
 */ 
function getVillageName($cid){
    $result = M('community')->field('name')->where(array('id'=>$cid))->find();
    return $result['name'];
}

/**
 * 根据小区id获取省份，城市
 * 
 */ 
function getCity($id){
    $Community = M('community');
    $City = M('city');
    $cid = $Community->field('cid')->where(array('id'=>$id))->find();
    $village = $Community->field('id,name')->where(array('cid'=>$cid['cid']))->order('list desc')->select();
    $province = $City->field('fup')->where(array('fid'=>$cid['cid']))->order('list desc')->find();
    $city2 = $City->where(array('fup'=>$province['fup']))->order('list desc')->select();
    $result['province'] = $province['fup'];
    $result['city'] = $city2;
    $result['cityid'] = $cid['cid'];
    $result['village'] = $village;
    return $result;
}

//返回AJAX弹窗
function output($data, $type = 'json', $exit = true, $extra = '')
{
    switch ($type)
    {
        case 'debug' :
            echo '<pre>';
            print_r($data);
            echo '</pre>';
            break;
        case 'xml' :
            // echo $this->_toXml();
            break;
        case 'json' :
        default :
            if (isset($_GET['callback'])) {
                $jsonp = $_GET['callback'];
                echo $jsonp . '(' . json_encode($data, JSON_UNESCAPED_UNICODE) . ')';
            } else {
                echo json_encode($data, JSON_UNESCAPED_UNICODE);
            }
    }

    if ($exit)
    {
        exit();
    }
    return true;
}

/**
 * 根据城市id获取城市名
 * 
 */
function getCityName($id){
    $name = M('city')->field('name')->where(array('fid'=>$id))->find();
    return $name['name'];
}

//模拟POST提交
function justdopostcurl($posturl,$post_data)//curl传送参数并获得信息
{
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$posturl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
    $data = curl_exec($ch);//运行curl
    curl_close($ch);
    return $data;
}

//报修满意
function mayi($member)
{
    switch ($member)
    {
        case 1;
            $str = '不满意';
            break;
        case 2;
            $str = '基本满意';
            break;
        case 3;
            $str = '满意';
            break;
        case 4;
            $str = '很满意';
            break;
    }
    return $str;
}

//报修状态
function zhuangtai($member)
{
    switch ($member)
    {
        case 0;
            $str = '未受理';
            break;
        case 1;
            $str = '处理中';
            break;
        case 2;
            $str = '处理完';
            break;
        case 4;
            $str = '已关闭';
            break;
    }
    return $str;
}

//评论星级
function star_level($member)
{
    switch ($member)
    {
//        case '';
//            $array = [
//                    'str' => '',
//                    '1' => '<a class="fa fa-star-o text-info" title="不满意"></a>',
//                    '2' => '<a class="fa fa-star-o text-info" title="基本满意"></a>',
//                    '3' => '<a class="fa fa-star-o text-info" title="满意"></a>',
//                    '4' => '<a class="fa fa-star-o text-info"  title="非常满意"></a>',
//            ];
//            break;
        case 1;
            $array = [
                'str' => '不满意',
                '1' => '<a class="fa fa-star text-info" title="不满意"></a><a class="fa fa-star-o text-info" title="基本满意"></a><a class="fa fa-star-o text-info" title="满意"></a><a class="fa fa-star-o text-info"  title="非常满意"></a>',

            ];
            break;
        case 2;
            $array = [
                    'str' => '基本满意',
                    '1' => '<a class="fa fa-star text-info" title="不满意"></a><a class="fa fa-star text-info"  title="基本满意"></a><a class="fa fa-star-o text-info"  title="满意"></a><a class="fa fa-star-o text-info"  title="非常满意"></a>',

            ];
            break;
        case 3;
            $array = [
                    'str' => '满意',
                    '1' => '<a class="fa fa-star text-info" title="不满意"></a><a class="fa fa-star text-info"  title="基本满意"></a><a class="fa fa-star text-info" title="满意"></a><a class="fa fa-star-o text-info"  title="非常满意"></a>',
            ];
            break;
        case 4;
            $array = [
                    'str' => '非常满意',
                    '1' => '<a class="fa fa-star text-info" title="不满意"></a><a class="fa fa-star text-info"  title="基本满意"></a><a class="fa fa-star text-info" title="满意"></a><a class="fa fa-star text-info"  title="非常满意"></a>',
            ];
            break;
    }
    return $array;
}

//图片处理
/*
 * $picture 图片字符串
 * $size 图片大小
 * $bulk 图片大小
 * $number 取图格式
 */
function picture($picture,$size,$bulk,$number)
{
    $service = [];
    if ($picture == '')
    {
        $service['thumb'][] = "<img style='$size' src='http://$_SERVER[SERVER_NAME]/Uploads/touxiang/2.png'>";
		$service['prompt']  = 1;
    }
    else
    {
        $url = $_SERVER['SERVER_NAME'];
        if (preg_match('#^http\://#is', $picture))
        {
            $picture = explode('@@@', $picture);
            switch ($number)
            {
                case '1':
                    foreach($picture as $v)
                    {
                        $service['thumb'][]= "<div class=\"col-sm-3\" style='$bulk'><a class='m-t-xs img-responsive' href='$v' target='_blank'><img src='$v'></a></div>";
                    }
                    break;
                case '2':
                    foreach($picture as $v)
                    {
                        $service['thumb'][]= "<a target=\"_blank\" href='$v'><img src='$v'></a>";
                    }
                    break;
            }
        }
        elseif (preg_match('@^/Uploads@', $picture))
        {
            $picture = explode('@@@', $picture);
            switch ($number)
            {
                case '1':
                    foreach($picture as $v)
                    {
                        $service['thumb'][]= "<div class=\"col-sm-3\" style='$bulk'><a class='m-t-xs img-responsive' href='http://$url/$v' target='_blank'><img src='http://$url/$v'></a></div>";
                    }
                    break;
                case '2':
                    foreach($picture as $v)
                    {
                        $service['thumb'][]= "<a target=\"_blank\" href='http://$url/$v'><img src='http://$url/$v'></a>";
                    }
                    break;
            }

        }
        else
        {
            $picture = explode('@@@', $picture);
            switch ($number)
            {
                case '1':
                    foreach($picture as $v)
                    {
                        $service['thumb'][]= "<div class=\"col-sm-3\" style='$bulk'><a class='m-t-xs img-responsive' href='http://".$url."/zj/upload_files//$v' target='_blank'><img src='http://".$url."/zj/upload_files/$v'></a></div>";
                    }
                    break;
                case '2':
                    foreach($picture as $v)
                    {
                        $service['thumb'][]= "<a target=\"_blank\" href='http://".$url."/zj/upload_files/$v'><img src='http://".$url."/zj/upload_files//$v'></a>";
                    }
                    break;
            }
        }
    }
    return $service;
}

//轮播图图片处理
/*
 * $picture 图片字符串
 */
function picture2($picture)
{
    if ($picture == '')
    {
        $service['thumb'][] = "";
        $service['prompt']  = 1;
    }
    else
    {
        $url = $_SERVER['SERVER_NAME'];
        if (preg_match('#^http\://#is', $picture))
        {
            
			$img_url= $picture;
			
        }
        elseif (preg_match('@^/Uploads@', $picture))
        {
            $img_url= 'http://'.$url.$picture;
			

        }
        else
        {
              
			$img = explode('|', $picture);
			$img_url= 'http://'.$url.'/zj/upload_files/'.$img[0];
		
			
        }
    }
    return $img_url;
}

function is_error($data) {
	if (empty($data) || !is_array($data) || !array_key_exists('errno', $data) || (array_key_exists('errno', $data) && $data['errno'] == 0)) {
		return false;
	} else {
		return true;
	}
}

//短信return
function return_sms($number)
{
    switch ($number)
    {
        case '0':
            output(['title'=>'发送失败','type'=>'error','text'=>'提交成功']);
            break;
        case '-1':
            output(['title'=>'发送失败','type'=>'error','text'=>'参数传递错误']);
            break;
        case '-2':
            output(['title'=>'发送失败','type'=>'error','text'=>'号码超过50000个']);
            break;
        case '-3':
            output(['title'=>'发送失败','type'=>'error','text'=>'内容超过70字']);
            break;
        case '-4':
            output(['title'=>'发送失败','type'=>'error','text'=>'内容含敏感词']);
            break;
        case '-5':
            output(['title'=>'发送失败','type'=>'error','text'=>'用户名或密码错误']);
            break;
        case '-6':
            output(['title'=>'发送失败','type'=>'error','text'=>'账户余额不足']);
            break;
        case '-7':
            output(['title'=>'发送失败','type'=>'error','text'=>'短信提交失败']);
            break;
        case '-8':
            output(['title'=>'发送失败','type'=>'error','text'=>'网络连接错误']);
            break;
        case '-9':
            output(['title'=>'发送失败','type'=>'error','text'=>'帐户未开通']);
            break;
        case '-10':
            output(['title'=>'发送失败','type'=>'error','text'=>'通道ID有误']);
            break;
    }
}

function superman_get_distance($lat1, $lng1, $lat2, $lng2){
    $earthRadius = 6367000;
    $lat1 = ($lat1 * pi()) / 180;
    $lng1 = ($lng1 * pi()) / 180;
    $lat2 = ($lat2 * pi()) / 180;
    $lng2 = ($lng2 * pi()) / 180;
    $calcLongitude = $lng2 - $lng1;
    $calcLatitude = $lat2 - $lat1;
    $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
    $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
    $calculatedDistance = $earthRadius * $stepTwo;
    return round($calculatedDistance);
}

//获取坐标代码
function tpl_form_field_coordinate($field, $value = array()) {
	$url = $_SERVER['SERVER_NAME'];

	$s = '';
    if(!defined('TPL_INIT_COORDINATE')) {
        $s .= '<script type="text/javascript">
				function showCoordinate(elm) {
					require(["http://'.$url.'/Public/admin/js/util.js"], function(util){
						var val = {};
						val.lng = parseFloat($(elm).parent().prev().prev().find(":text").val());
						val.lat = parseFloat($(elm).parent().prev().find(":text").val());
						util.map(val, function(r){
							$(elm).parent().prev().prev().find(":text").val(r.lng);
							$(elm).parent().prev().find(":text").val(r.lat);
						});

					});
				}

			</script>';
        define('TPL_INIT_COORDINATE', true);
    }
    $s .= '
		<div class="row row-fix">
			<div class="col-xs-4 col-sm-4">
				<input type="text" name="' . $field . '[lng]" value="'.$value['lng'].'" placeholder="地理经度"  class="form-control" />
			</div>
			<div class="col-xs-4 col-sm-4">
				<input type="text" name="' . $field . '[lat]" value="'.$value['lat'].'" placeholder="地理纬度"  class="form-control" />
			</div>
			<div class="col-xs-4 col-sm-4">
				<button onclick="showCoordinate(this);" class="btn btn-default" type="button">选择坐标</button>
			</div>
		</div>';
    return $s;
}

//车辆属性
function nature($number)
{
    $a = '业主';//1
    $b = '租客';//2
    $c = '临时车辆';//3
    switch ($number)
    {
        case '1':
            $return = "
                        <option  value=\"-1\" >车辆属性</option>
						<option selected='selected' value=\"1\" style=\"color: red\">$a</option>
						<option value=\"2\" style=\"color: #0bb20c\">$b</option>
                        <option value=\"3\" style=\"color: #0000cc\">$c</option>";
            break;
        case '2':
            $return = "
                        <option  value=\"-1\" >车辆属性</option>
    					<option value=\"1\" style=\"color: red\">$a</option>
						<option selected='selected' value=\"2\" style=\"color: #0bb20c\">$b</option>
                        <option value=\"3\" style=\"color: #0000cc\">$c</option>";
            break;
        case '3':
            $return = "
                        <option  value=\"-1\">车辆属性</option>
						<option  value=\"1\" style=\"color: red\">$a</option>
						<option value=\"2\" style=\"color: #0bb20c\">$b</option>
                        <option selected='selected' value=\"3\" style=\"color: #0000cc\">$c</option>"
        ;
            break;
        default:
            $return =  "<option selected='selected' value=\"-1\" >车辆属性</option>
						<option  value=\"1\" style=\"color: red\">$a</option>
						<option value=\"2\" style=\"color: #0bb20c\">$b</option>
                        <option  value=\"3\" style=\"color: #0000cc\">$c</option>"
            ;
    };
    return $return;
}
//车辆属性
function nature_span($number)
{
    $a = '业主';//1
    $b = '租客';//2
    $c = '临时车辆';//3
    switch ($number)
    {
        case '1':
            $return = "<span  style=\"color: red\">$a</span>";
            break;
        case '2':
            $return = "<span   style=\"color: #0bb20c\">$b</span>";
            break;
        case '3':
            $return = " <span  style=\"color: #0000cc\">$c</span>"
            ;
            break;
        default:
            $return ="<span style='color: #BBB5B5'>暂未录入</span>"
            ;
    };
    return $return;
}

//车辆类型
function vehicle_type($number)
{
    $a = '小车';//1
    $b = '面包车';//2
    $c = '越野车';//3
    switch ($number)
    {
        case '1':
            $return = "
<option  value=\"-1\" style=\"color: red\">车辆类型</option>
						<option selected='selected' value=\"1\">$a</option>
						<option value=\"2\" >$b</option>
                        <option value=\"3\">$c</option>";
            break;
        case '2':
            $return = "
<option ' value=\"-1\" style=\"color: red\">车辆类型</option>
						<option value=\"1\">$a</option>
						<option selected='selected' value=\"2\" >$b</option>
                        <option value=\"3\" >$c</option>";
            break;
        case '3':
            $return = "
<option value=\"-1\" style=\"color: red\">车辆类型</option>
						<option  value=\"1\" >$a</option>
						<option value=\"2\">$b</option>
                        <option selected='selected' value=\"3\" >$c</option>";
            break;
        default:
            $return ="
<option selected='selected' value=\"-1\" style=\"color: red\">车辆类型</option>
<option  value=\"1\" >$a</option>
						<option value=\"2\">$b</option>
                        <option  value=\"3\" >$c</option>";
    };
    return $return;
}

//车辆类型
function vehicle_type_span($number)
{
    $a = '小车';//1
    $b = '面包车';//2
    $c = '越野车';//3
    switch ($number)
    {
        case '1':
            $return = "<span >$a</span>";

            break;
        case '2':
            $return = "<span>$b</span>";

            break;
        case '3':
            $return = " <span >$c</span>"
            ;
            break;
        default:
            $return ="<span style='color: #BBB5B5'>暂未录入</span>"
            ;
    };
    return $return;
}

//是否审核
function if_Auditing($number)
{
    switch ($number)
    {
        case '1':
            $return = "<option value=\"-1\">审核状态</option>
						<option selected='selected' value=\"1\" style=\"color: #0bb20c\">已审核</option>
						<option value=\"0\" style=\"color: #314D67\">未审核</option>"
            ;
            break;
        case '0':
            $return = "<option value=\"-1\">审核状态</option>
						<option value=\"1\" style=\"color: #0bb20c\">已审核</option>
						<option selected='selected' value=\"0\" style=\"color: #314D67\">未审核</option>"
            ;
            break;
        default:
            $return = "<option selected='selected' value=\"-1\">审核状态</option>
						<option value=\"1\" style=\"color: #0bb20c\">已审核</option>
						<option value=\"0\" style=\"color: #314D67\">未审核</option>"
            ;
    };
    return $return;
}

//是否审核
function source($number)
{
    $a = 'APP';//1
    $b = '科拓';//2
    $c = '后台';//3
    switch ($number)
    {
        case '1':
            $return = "<span style='color: #0bb20c'>$a</span>";

            break;
        case '2':
            $return = "<span style='color: #0000cc'>$b</span>";

            break;
        case '3':
            $return = " <span style='color: red'>$c</span>"
            ;
            break;
        default:
            $return ="<span style='color: #BBB5B5'>暂未录入</span>"
            ;
    };
    return $return;
}

//是否审核
function if_Auditing_select($number)
{
    switch ($number)
    {
        case '1':
            $return = "<option value=\"-1\">审核状态</option>
						<option selected='selected' value=\"1\" style=\"color: #0bb20c\">已审核</option>
						<option value=\"0\" style=\"color: #314D67\">未审核</option>"
            ;
            break;
        case '0':
            $return = "<option value=\"-1\">审核状态</option>
						<option value=\"1\" style=\"color: #0bb20c\">已审核</option>
						<option selected='selected' value=\"0\" style=\"color: #314D67\">未审核</option>"
            ;
            break;
        default:
            $return = "<option selected='selected' value=\"-1\">审核状态</option>
						<option value=\"1\" style=\"color: #0bb20c\">已审核</option>
						<option value=\"0\" style=\"color: #314D67\">未审核</option>"
            ;
    };
    return $return;
}

//车位图显示颜色
function bgcolor($res,$data){
    $bgcolor = '';
    if ($res['parking_xiaoshou'] == 1 && $res['parking_chuzu'] == 2) {
        $bgcolor = 'class="bg02"';
    }
    if ($res['parking_xiaoshou'] == 2 && $res['parking_jieshou'] == 1 && $res['parking_chuzu'] == 2) {
        $bgcolor = 'class="bg03"';
    }
    if ($res['parking_xiaoshou'] == 2 && $res['parking_jieshou'] == 1 && $res['parking_chuzu'] == 1) {
        $bgcolor = 'class="bg04"';
    }
    if ($res['parking_xiaoshou'] == 2 && $res['parking_jieshou'] == 2) {
        $bgcolor = 'class="bg05"';
    }
    $name1 = trim($data[$res['room_code']]['name']);
    $name2 = trim($data[$res['room_code']]['purchaser']);
    if ($name1 != $name2 && !empty($data[$res['room_code']]['purchaser']) && empty($res['parking_chanquan'])) {
        $bgcolor = 'class="bg06"';
    }
    return $bgcolor;
}

?>
